﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml;
using LibraLab.LL_System;

namespace Web_Oto.Bases
{
    public abstract class dalObject
    {
        private bool m_bUseCache;
        private CommandType m_cmdType;
        private string m_connectionString;
        private string m_connectionType;
        private int m_intCommandTimeOut;
        private MSSQL objMsSQL;

        public dalObject(string newConnectionString, string newConnectionType)
        {
            this.m_bUseCache = true;
            this.ConnectionString = newConnectionString;
            this.ConnectionType = newConnectionType;
            this.cmdType = CommandType.StoredProcedure;
            if ((HttpContext.Current != null) && (HttpContext.Current.Session != null))
            {
                this.m_bUseCache = HttpContext.Current.Session["noCache"] == null;
            }
            if ((ConfigurationManager.AppSettings["CommandTimeOut"] != null) && !string.IsNullOrEmpty(ConfigurationManager.AppSettings["CommandTimeOut"].ToString()))
            {
                this.CommandTimeOut = Convert.ToInt32(ConfigurationManager.AppSettings["CommandTimeOut"].ToString());
            }
            string connectionType = this.ConnectionType;
            this.objMsSQL = new MSSQL(this.ConnectionString);

        }

        public dalObject(IDbConnection sqlConnection, string newConnectionType, SqlTransaction dbTransaction)
        {
            this.m_bUseCache = true;
            this.ConnectionType = newConnectionType;
            this.cmdType = CommandType.StoredProcedure;
            if ((HttpContext.Current != null) && (HttpContext.Current.Session != null))
            {
                this.m_bUseCache = HttpContext.Current.Session["noCache"] == null;
            }
            if ((ConfigurationManager.AppSettings["CommandTimeOut"] != null) && !string.IsNullOrEmpty(ConfigurationManager.AppSettings["CommandTimeOut"].ToString()))
            {
                this.CommandTimeOut = Convert.ToInt32(ConfigurationManager.AppSettings["CommandTimeOut"].ToString());
            }
            string connectionType = this.ConnectionType;
            this.objMsSQL = new MSSQL(sqlConnection);
            this.objMsSQL.sqlTransaction = dbTransaction;
        }

        protected string BuildStoredProcCommand(string strCommandText, IDataParameter[] parameters)
        {
            return this.objMsSQL.BuildStoredProcCommand(strCommandText, parameters);
        }

        protected void CloseConnection()
        {
            string connectionType = this.ConnectionType;
            this.objMsSQL.CloseConnection();
        }

        protected string createWhere(string strWhere)
        {
            string connectionType = this.ConnectionType;
            return this.objMsSQL.createWhere(strWhere);
        }

        protected byte[] ExecByteCommand(string strCommandText)
        {
            this.objMsSQL.cmdType = this.cmdType;
            return this.objMsSQL.ExecByteCommand(strCommandText);
        }

        protected byte[] ExecByteCommand(string strCommandText, IDataParameter[] parameters)
        {
            this.objMsSQL.cmdType = CommandType.StoredProcedure;
            return this.objMsSQL.ExecByteCommand(strCommandText, parameters);
        }

        protected string ExecCacheCommand(string strQuery, string strCommandText, IDataParameter[] parameters, ref bool blnFromCache)
        {
            return this.ExecCacheCommand(strQuery, strCommandText, parameters, "", ref blnFromCache, this.UseCache);
        }

        protected string ExecCacheCommand(string strQuery, string strCommandText, IDataParameter[] parameters, string strCommandtype, ref bool blnFromCache)
        {
            return this.ExecCacheCommand(strQuery, strCommandText, parameters, strCommandtype, ref blnFromCache, this.UseCache);
        }

        protected string ExecCacheCommand(string strQuery, string strCommandText, IDataParameter[] parameters, string strCommandtype, ref bool blnFromCache, bool blnUseCache)
        {
            string xml = "";
            blnFromCache = true;
            SqlParameter[] parameterArray = new SqlParameter[] { new SqlParameter("@strQuery", SqlDbType.VarChar, strQuery.Length + 1), new SqlParameter("@intUseCache", SqlDbType.Int, 4) };
            parameterArray[0].Value = strQuery;
            parameterArray[1].Value = blnUseCache ? 1 : 0;
            this.cmdType = CommandType.StoredProcedure;
            int intCommandTimeout = 0;
            if ((ConfigurationManager.AppSettings["CommandTimeOut"] != null) && !string.IsNullOrEmpty(ConfigurationManager.AppSettings["CommandTimeOut"].ToString()))
            {
                intCommandTimeout = Convert.ToInt32(ConfigurationManager.AppSettings["CommandTimeOut"].ToString());
            }
            xml = this.ExecStringCommand("lbl_cache_get", parameterArray);
            if (xml.Length != 0)
            {
                return xml;
            }
            string str2 = strCommandtype;
            if (str2 != null)
            {
                if (!(str2 == "text"))
                {
                    if (str2 == "dataset")
                    {
                        xml = this.objMsSQL.ExecDataSetCommand(strCommandText, parameters).GetXml();
                        goto Label_0127;
                    }
                }
                else
                {
                    this.objMsSQL.cmdType = CommandType.Text;
                    xml = this.objMsSQL.ExecXMLCommand(strCommandText, parameters, intCommandTimeout);
                    goto Label_0127;
                }
            }
            xml = this.objMsSQL.ExecXMLCommand(strCommandText, parameters, intCommandTimeout);
        Label_0127:
            blnFromCache = false;
            return xml;
        }

        protected bool ExecCacheDeleteCommand(string strQuery)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@strQuery", SqlDbType.VarChar, strQuery.Length + 1) };
            parameters[0].Value = strQuery;
            this.objMsSQL.cmdType = CommandType.StoredProcedure;
            this.ExecVoidCommand("lbl_cache_del", parameters);
            return true;
        }

        protected bool ExecCacheUpdateCommand(string strQuery, string strResult)
        {
            if (strResult.Length > 0)
            {
                SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@strQuery", SqlDbType.VarChar, strQuery.Length + 1), new SqlParameter("@strResult", SqlDbType.Text, strResult.Length + 1) };
                parameters[0].Value = strQuery;
                parameters[1].Value = strResult;
                this.ExecVoidCommand("lbl_cache_upd", parameters);
                return true;
            }
            return false;
        }

        protected DataRow ExecDataRowCommand(string strCommandText)
        {
            string connectionType = this.ConnectionType;
            this.objMsSQL.cmdType = this.cmdType;
            return this.objMsSQL.ExecDataRowCommand(strCommandText);
        }

        protected DataRow ExecDataRowCommand(string strCommandText, IDataParameter[] parameters)
        {
            string connectionType = this.ConnectionType;
            this.objMsSQL.cmdType = this.cmdType;
            return this.objMsSQL.ExecDataRowCommand(strCommandText, parameters);
        }

        protected DataSet ExecDataSetCommand(string strCommandText)
        {
            string connectionType = this.ConnectionType;
            this.objMsSQL.cmdType = this.cmdType;
            return this.objMsSQL.ExecDataSetCommand(strCommandText);
        }

        protected DataSet ExecDataSetCommand(string strCommandText, IDataParameter[] parameters)
        {
            string connectionType = this.ConnectionType;
            this.objMsSQL.cmdType = this.cmdType;
            return this.objMsSQL.ExecDataSetCommand(strCommandText, parameters);
        }

        protected DateTime ExecDateTimeCommand(string strCommandText)
        {
            this.objMsSQL.cmdType = this.cmdType;
            return this.objMsSQL.ExecDateTimeCommand(strCommandText);
        }

        protected DateTime ExecDateTimeCommand(string strCommandText, IDataParameter[] parameters)
        {
            this.objMsSQL.cmdType = this.cmdType;
            return this.objMsSQL.ExecDateTimeCommand(strCommandText, parameters, this.CommandTimeOut);
        }

        protected double ExecDoubleCommand(string strCommandText)
        {
            this.objMsSQL.cmdType = this.cmdType;
            return this.objMsSQL.ExecDoubleCommand(strCommandText);
        }

        protected double ExecDoubleCommand(string strCommandText, IDataParameter[] parameters)
        {
            this.objMsSQL.cmdType = this.cmdType;
            return this.objMsSQL.ExecDoubleCommand(strCommandText, parameters, this.CommandTimeOut);
        }

        protected int ExecIntCommand(string strCommandText)
        {
            string connectionType = this.ConnectionType;
            this.objMsSQL.cmdType = this.cmdType;
            return this.objMsSQL.ExecIntCommand(strCommandText);
        }

        protected int ExecIntCommand(string strCommandText, IDataParameter[] parameters)
        {
            string connectionType = this.ConnectionType;
            this.objMsSQL.cmdType = this.cmdType;
            return this.objMsSQL.ExecIntCommand(strCommandText, parameters, this.CommandTimeOut);
        }

        protected string ExecStringCommand(string strCommandText)
        {
            string connectionType = this.ConnectionType;
            this.objMsSQL.cmdType = this.cmdType;
            return this.objMsSQL.ExecStringCommand(strCommandText);
        }

        protected string ExecStringCommand(string strCommandText, IDataParameter[] parameters)
        {
            string connectionType = this.ConnectionType;
            this.objMsSQL.cmdType = this.cmdType;
            return this.objMsSQL.ExecStringCommand(strCommandText, parameters, this.CommandTimeOut);
        }

        protected string ExecStringCommand(string strCommandText, IDataParameter[] parameters, int intCommandTimeout)
        {
            this.objMsSQL.cmdType = this.cmdType;
            return this.objMsSQL.ExecStringCommand(strCommandText, parameters, intCommandTimeout);
        }

        protected void ExecVoidCommand(string strCommandText)
        {
            string connectionType = this.ConnectionType;
            this.objMsSQL.cmdType = this.cmdType;
            this.objMsSQL.ExecVoidCommand(strCommandText);
        }

        protected void ExecVoidCommand(string strCommandText, IDataParameter[] parameters)
        {
            string connectionType = this.ConnectionType;
            this.objMsSQL.cmdType = this.cmdType;
            this.objMsSQL.ExecVoidCommand(strCommandText, parameters, this.CommandTimeOut);
        }

        protected string ExecXMLCommand(string strCommandText)
        {
            string connectionType = this.ConnectionType;
            this.objMsSQL.cmdType = this.cmdType;
            this.objMsSQL.ConnectionString = this.ConnectionString;
            return this.objMsSQL.ExecXMLCommand(strCommandText);
        }

        protected string ExecXMLCommand(string strCommandText, IDataParameter[] parameters)
        {
            string connectionType = this.ConnectionType;
            this.objMsSQL.cmdType = this.cmdType;
            this.objMsSQL.ConnectionString = this.ConnectionString;
            return this.objMsSQL.ExecXMLCommand(strCommandText, parameters, this.CommandTimeOut);
        }

        protected string ExecXMLCommand(string strCommandText, IDataParameter[] parameters, int intCommandTimeout)
        {
            this.objMsSQL.cmdType = this.cmdType;
            this.objMsSQL.ConnectionString = this.ConnectionString;
            return this.objMsSQL.ExecXMLCommand(strCommandText, parameters, intCommandTimeout);
        }

        protected XmlReader ExecXMLDataReaderCommand(string strCommandText)
        {
            this.objMsSQL.cmdType = this.cmdType;
            this.objMsSQL.ConnectionString = this.ConnectionString;
            return this.objMsSQL.ExecXMLDataReaderCommand(strCommandText);
        }

        protected XmlReader ExecXMLDataReaderCommand(string strCommandText, IDataParameter[] parameters)
        {
            this.objMsSQL.cmdType = this.cmdType;
            this.objMsSQL.ConnectionString = this.ConnectionString;
            return this.objMsSQL.ExecXMLDataReaderCommand(strCommandText, parameters, this.CommandTimeOut);
        }

        protected XmlReader ExecXMLDataReaderCommand(string strCommandText, IDataParameter[] parameters, int intCommandTimeout)
        {
            this.objMsSQL.cmdType = this.cmdType;
            this.objMsSQL.ConnectionString = this.ConnectionString;
            return this.objMsSQL.ExecXMLDataReaderCommand(strCommandText, parameters, intCommandTimeout);
        }

        protected XmlDocument ExecXMLDocumentCommand(string strCommandText)
        {
            string connectionType = this.ConnectionType;
            this.objMsSQL.cmdType = this.cmdType;
            this.objMsSQL.ConnectionString = this.ConnectionString;
            return this.objMsSQL.ExecXMLDocumentCommand(strCommandText);
        }

        protected XmlDocument ExecXMLDocumentCommand(string strCommandText, IDataParameter[] parameters)
        {
            string connectionType = this.ConnectionType;
            this.objMsSQL.cmdType = this.cmdType;
            this.objMsSQL.ConnectionString = this.ConnectionString;
            return this.objMsSQL.ExecXMLDocumentCommand(strCommandText, parameters, this.CommandTimeOut);
        }

        protected XmlDocument ExecXMLDocumentCommand(string strCommandText, IDataParameter[] parameters, int intCommandTimeout)
        {
            this.objMsSQL.cmdType = this.cmdType;
            this.objMsSQL.ConnectionString = this.ConnectionString;
            return this.objMsSQL.ExecXMLDocumentCommand(strCommandText, parameters, intCommandTimeout);
        }

        protected void OpenConnection()
        {
            string connectionType = this.ConnectionType;
            this.objMsSQL.OpenConnection();
        }

        public CommandType cmdType
        {
            get
            {
                return this.m_cmdType;
            }
            set
            {
                this.m_cmdType = value;
            }
        }

        protected int CommandTimeOut
        {
            get
            {
                return this.m_intCommandTimeOut;
            }
            set
            {
                this.m_intCommandTimeOut = value;
            }
        }

        public IDbConnection ConnectionDb
        {
            get
            {
                string connectionType = this.ConnectionType;
                return this.objMsSQL.sqlConnection;
            }
        }

        protected string ConnectionString
        {
            get
            {
                return this.m_connectionString;
            }
            set
            {
                this.m_connectionString = value;
            }
        }

        protected string ConnectionType
        {
            get
            {
                return this.m_connectionType;
            }
            set
            {
                this.m_connectionType = value;
            }
        }

        public SqlTransaction sqlTransaction
        {
            get
            {
                string connectionType = this.ConnectionType;
                return this.objMsSQL.sqlTransaction;
            }
            set
            {
                string connectionType = this.ConnectionType;
                this.objMsSQL.sqlTransaction = value;
            }
        }

        protected bool UseCache
        {
            get
            {
                return this.m_bUseCache;
            }
            set
            {
                this.m_bUseCache = value;
            }
        }


    }
}
